Java syntax
part 12/46 · 86.7 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Control structures
Conditional statements
if statement
if statements in Java are similar to those in C and use the same syntax:
if (i == 3) {
doSomething();
}
if statement may include optional else block, in which case it becomes an if-then-else statement:
if (i == 3) {
doSomething();
} else {
doSomethingElse();
}
Like C, else-if construction does not involve any special keywords, it is formed as a sequence of separate if-then-else statements:
if (i == 3) {
doSomething();
} else if (i == 2) {
doSomethingElse();
} else {
doSomethingDifferent();
}
Also, a ?: operator can be used in place of simple if statement, for example
int a = 1;
int b = 2;
int minVal = (a < b) ? a : b;
switch statement
Switch statements in Java can use byte, short, char, and int (not long) primitive data types or their corresponding wrapper types. Starting with J2SE 5.0, it is possible to use enum types. Starting with Java SE 7, it is possible to use Strings.cite-ref-2[2] Other reference types cannot be used in switch statements.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────